﻿using System;
using System.Collections.Generic;
using FileSquirrel.Logic;
using System.ComponentModel;


namespace FSquirrel.Design
{
    public class FileRowModel : INotifyPropertyChanged
    {
        private FileNameHelper _helper;
        private string name;
        private readonly List<char> _alphabet = new List<char>("ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray());

        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                _helper = new FileNameHelper(name);
            }
        }

        public string LibraryName
        {
            get { return "Library"; }
        }

        public int Size { get; set; }


        public TimeSpan Duration { get; set; }

        public string Category { get; set; }

        public string Path { get; set; }

        public DateTime DateModified { get; set; }

        public string Extension { get; set; }

        public string Resolution { get; set; }

        public string Show { get; set; }

        public string Season { get; set; }

        public string EpisodeNr { get; set; }

        private bool isselected;
        public bool IsSelected
        {
            get { return isselected; }
            set
            {
                isselected = value;
                RaisePropertyChanged("IsSelected");
            }
        }

        private bool _isrequested;
        public bool IsRequested
        {
            get { return _isrequested; }
            set
            {
                _isrequested = value;
                RaisePropertyChanged("IsRequested");
            }
        }

        public string ShowName
        {
            get
            {
                //Only series get show name
                if (Category.Equals("Series", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (string.IsNullOrEmpty(_helper.Show))
                    {
                        return "#";
                    }
                    return CleanText(_helper.Show);
                }
                //Movies etc only alphabet else #
                var shortname = Name.Substring(0, 1).ToUpper();
                if (_alphabet.Contains(shortname[0]))
                {
                    return Name.Substring(0, 1).ToUpper();
                }
                return "#";
            }
        }

        public string ShowSeason
        {
            get
            {
                if (Category.Equals("Series", StringComparison.InvariantCultureIgnoreCase))
                {
                    return _helper.Season;
                }
                return null;
            }
        }

        private string CleanText(string dirtyText)
        {
            string _clean = dirtyText.Replace(".", "").Replace("[", "").Replace("]", "");

            if (_clean.ToUpperInvariant().StartsWith("THE "))
            {
                _clean = _clean.Substring(3, _clean.Length - 3).Trim() + ", The";
            }

            if (_clean.ToUpperInvariant().StartsWith("A "))
            {
                _clean = _clean.Substring(1, _clean.Length - 1).Trim() + ", A";
            }

            return _clean;
        }



        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }

        #endregion


    }
}